from typing import List


class Solution:
    def waysToMakeFair(self, nums: List[int]) -> int:
        all = 0
        len_nums = len(nums)

        for i in range(len_nums):
            odd = 0
            even = 0
            for j in range(len_nums):
                if j ==i:
                    continue
                if j>i:
                    if (j - 1) % 2 == 0:
                        even +=nums[j]
                    else:
                        odd +=nums[j]
                else:
                    if j % 2 == 0:
                        even +=nums[j]
                    else:
                        odd +=nums[j]

            if odd == even:
                all += 1
        return all

    def waysToMakeFair1(self, nums: List[int]) -> int:
        ans = 0
        n = len(nums)
        sum_odd = sum(nums[1:n:2])  # 所有奇数下标元素和
        sum_even = sum(nums[0:n:2])  # 所有偶数下标元素和
        left_odd = 0  # i左边奇数下标元素和
        left_even = 0  # i左边偶数下标元素和

        for i in range(n):
            # 删除i下标元素
            if i % 2:
                # 当前是奇数下标，sum_odd需要删除nums[i]
                if left_even + (sum_odd - left_odd - nums[i]) == left_odd + (
                        sum_even - left_even):  # 左边的偶下标加右边的奇下标是否等于左边的奇下标加上右边的偶下标的元素和
                    ans += 1
                left_odd += nums[i]  # 累加左边奇数下标元素和
            else:
                # 当前是偶数下标，sum_even需要删除nums[i]
                if left_even + (sum_odd - left_odd) == left_odd + (sum_even - left_even - nums[i]):
                    ans += 1
                left_even += nums[i]  # 累加左边偶数下标元素和
        return ans




if __name__ == '__main__':
    nums = [2,1,6,4]
    solution = Solution()
    val = solution.waysToMakeFair(nums)
    print(val)


